﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BookStore.Bussiness;
using BookStore.Data;

namespace BookStore.admin
{
    public partial class Payment : System.Web.UI.Page
    {
        static bool flag = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["Administrator"])))
            {
                Response.Redirect("~/admin/Login.aspx");
            }
            showAll();
        }
        public void showAll()
        {
            gvPayment.DataSource = paymentService.getAll();
            gvPayment.DataBind();
        }
        public void ResetControl() {
            txtpaymentID.Text = "";
            txtPaymentName.Text = "";
            txtdescription.Text = "";
        }
        public void insertPayment()
        {
            paymentInfo objPayment = new paymentInfo();
            objPayment.paymentName = txtPaymentName.Text;
            objPayment.description = txtdescription.Text;
            bool test = paymentService.insertPayment(objPayment);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('insert success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('insert failed');</script>");
            }
        }
        public void updatePayment()
        {
            paymentInfo objPayment = new paymentInfo();
            objPayment.paymentID = Convert.ToInt32(txtpaymentID.Text);
            objPayment.paymentName = txtPaymentName.Text;
            objPayment.description = txtdescription.Text;
            bool test = paymentService.updatePayment(objPayment);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('update success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('update failed');</script>");
            }
        }
        protected void lbtBackT_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
            pnView.Visible = true;
        }

        protected void lbtAddT_Click(object sender, EventArgs e)
        {
            flag = true;
            ResetControl();
            pnUpdate.Visible = true;
            pnView.Visible = false;
        }

        protected void lbtRefreshB_Click(object sender, EventArgs e)
        {
            showAll();
        }

        protected void lbtUpdateT_Click(object sender, EventArgs e)
        {
            if (flag==true)
            {
                insertPayment();
            }
            else
            {
                updatePayment();
            }
            showAll();
            pnView.Visible = true;
            pnUpdate.Visible = false;
        }

        protected void lbtBack_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;
        }

        protected void gvPayment_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            flag = false;
            int paymentID = Convert.ToInt32(gvPayment.Rows[e.NewSelectedIndex].Cells[0].Text);
            paymentInfo objPayment = paymentService.getbyID(paymentID);
            txtpaymentID.Text = objPayment.paymentID.ToString();
            txtPaymentName.Text = objPayment.paymentName;
            txtdescription.Text = objPayment.description;
            pnUpdate.Visible = true;
            pnView.Visible = false;
        }

        protected void gvPayment_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int paymentID = Convert.ToInt32(gvPayment.Rows[e.RowIndex].Cells[0].Text);
            bool test = paymentService.deletePayment(paymentID);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('delete success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('delete failed');</script>");
            }
            showAll();
        }

        protected void gvPayment_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvPayment.PageIndex = e.NewPageIndex;
            showAll();
        }

    }
}
